package com.hepinghospital.user.ui.activity.consult;

import android.annotation.SuppressLint;
import android.content.Context;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseFragment;
import com.hepinghospital.user.eventbus.Logout;
import com.hepinghospital.user.ui.fragment.DignoseOnlineFragment;
import com.hepinghospital.user.ui.fragment.IMFragment;
import com.hepinghospital.user.ui.fragment.NurseConsultListFragment;
import com.rest.util.LogUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Objects;

import butterknife.BindView;
import jz.joyoung.robot.util.LogUtil;
import jz.joyoung.robot.util.UIConstants;

/**
 * Created by HP on 2020/2/20.
 */

public class JiuZhenFragment extends BaseFragment {
    @BindView(R.id.timeline_tablayout)
    TabLayout mTabLayout;
    @BindView(R.id.timeline_viewpager)
    ViewPager mViewPager;
    //缓存跳转的tab页位置
    public int alreadyJumpFrom = 0;
    //缓存是否已经跳转过
    public boolean alreadyJumpTo = false;

    @SuppressLint("StaticFieldLeak")
    private static JiuZhenFragment jiuZhenFragment;

    public ArrayList<String> titleArrayList = new ArrayList<String>();
    //图文问诊
    public IMFragment imFragment = IMFragment.newInstance();
    //护理咨询
    public NurseConsultListFragment nurseConsultListFragment = NurseConsultListFragment.newInstance();
    //云门诊
    public DignoseOnlineFragment dignoseOnlineFragment = DignoseOnlineFragment.newInstance();


    public static JiuZhenFragment newInstance() {
        if (jiuZhenFragment == null) {
            jiuZhenFragment = new JiuZhenFragment();
        }
        return jiuZhenFragment;
    }

    public DoctorFragmentAdapter getDoctorFragmentAdapter() {
        return doctorFragmentAdapter;
    }

    public DoctorFragmentAdapter doctorFragmentAdapter;

    @Override
    protected void initView() {
        doctorFragmentAdapter = new DoctorFragmentAdapter(this, mContext, getChildFragmentManager());
        mViewPager.setAdapter(doctorFragmentAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.setCurrentItem(UIConstants.JIUZHEN_TAB_POSITION_0);
        Objects.requireNonNull(mTabLayout.getTabAt(UIConstants.JIUZHEN_TAB_POSITION_0)).select();
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtil.e("JiuZhenFragment","回调了onResume方法");
        judgeDataPage();
        alreadyJumpTo = false;
    }

    /**
     * 判断哪个页面有数据
     */
    private void judgeDataPage() {
//        nurseConsultListFragment.queryNurseConsultList(UIConstants.JIUZHEN_QUERY_TYPE_1);
        dignoseOnlineFragment.getCloudList(UIConstants.JIUZHEN_QUERY_TYPE_1);
        imFragment.queryIMList(UIConstants.JIUZHEN_QUERY_TYPE_1);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    protected void initData() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        titleArrayList.add(getResources().getString(R.string.string_consult_graphic));
        titleArrayList.add(getResources().getString(R.string.string_consult_nurse));
        titleArrayList.add(getResources().getString(R.string.string_consult_cloud));
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_jiuzhen;
    }


    public static class DoctorFragmentAdapter extends FragmentPagerAdapter {

        private static final int PAGE_COUNT = 3;
        private Context context;
        private JiuZhenFragment jiuZhenFragment;

        DoctorFragmentAdapter(JiuZhenFragment jiuZhenFragment, Context context, FragmentManager fm) {
            super(fm);
            this.context = context;
            this.jiuZhenFragment = jiuZhenFragment;
        }

        @NotNull
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case UIConstants.JIUZHEN_TAB_POSITION_1://护理咨询
                    return jiuZhenFragment.nurseConsultListFragment;
                case UIConstants.JIUZHEN_TAB_POSITION_2://云门诊
                    return jiuZhenFragment.dignoseOnlineFragment;
                default:
                    return jiuZhenFragment.imFragment;
            }
        }

        @Override
        public int getCount() {
            return PAGE_COUNT;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case UIConstants.JIUZHEN_TAB_POSITION_1: {
                    return jiuZhenFragment.titleArrayList.get(UIConstants.JIUZHEN_TAB_POSITION_1);
                }
                case UIConstants.JIUZHEN_TAB_POSITION_2: {
                    return jiuZhenFragment.titleArrayList.get(UIConstants.JIUZHEN_TAB_POSITION_2);
                }
                default:
                    return jiuZhenFragment.titleArrayList.get(UIConstants.JIUZHEN_TAB_POSITION_0);
            }
        }

        public void setPageTitle(int position, int titleCount) {
            if (position >= 0 && position < jiuZhenFragment.titleArrayList.size()) {
                StringBuilder titleSb = new StringBuilder();
                switch (position) {
                    case UIConstants.JIUZHEN_TAB_POSITION_0: {
                        titleSb
                                .append(context.getResources().getString(R.string.string_consult_graphic))
                                .append("(")
                                .append(titleCount)
                                .append(")");
                        break;
                    }

                    case UIConstants.JIUZHEN_TAB_POSITION_1: {
                        titleSb
                                .append(context.getResources().getString(R.string.string_consult_nurse))
                                .append("(")
                                .append(titleCount)
                                .append(")");
                        break;
                    }

                    case UIConstants.JIUZHEN_TAB_POSITION_2: {
                        titleSb
                                .append(context.getResources().getString(R.string.string_consult_cloud))
                                .append("(")
                                .append(titleCount)
                                .append(")");
                        break;
                    }
                    default:
                        break;
                }
                jiuZhenFragment.titleArrayList.set(position, titleSb.toString());
                notifyDataSetChanged();
            }
        }
    }

    public synchronized void jumpToPage(int position) {
        if (position < alreadyJumpFrom || !alreadyJumpTo) {
            alreadyJumpTo = true;
            alreadyJumpFrom = position;
            mViewPager.setCurrentItem(position);
            mTabLayout.getTabAt(position).select();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            alreadyJumpTo = false;
            judgeDataPage();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(Logout event) {
        if (event.logout == 4) {
            mTabLayout.post(() -> {
                mViewPager.setCurrentItem(1);
                mTabLayout.getTabAt(1).select();
            });
        }
    }
}
